package com.todo.strategy.online.application.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.strategy.online.application.convert.StrategyConvert;
import com.todo.strategy.online.application.entity.bo.BasicStrategyBO;
import com.todo.strategy.online.application.entity.bo.StrategySearchBO;
import com.todo.strategy.online.application.service.BasicStrategyService;
import com.todo.strategy.online.infrastructure.helper.es.entity.BasicStrategyEntity;
import com.todo.strategy.online.infrastructure.helper.es.mapper.BasicStrategyEntityMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author azhebuxing
 * @date 2024/12/11 20:07
 * @description
 */
@Slf4j
@Service
public class BasicStrategyServiceImpl implements BasicStrategyService {

    @Resource
    private BasicStrategyEntityMapper basicStrategyEntityMapper;

    @Override
    public PageResult<BasicStrategyBO> searchStrategy(StrategySearchBO strategySearchBO) {
        if (strategySearchBO == null) {
            return PageResult.of(Collections.emptyList(), 0, 0, 0);
        }
        LambdaEsQueryWrapper<BasicStrategyEntity> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.multiMatchQuery(strategySearchBO.getKeyword(), BasicStrategyEntity::getTitle, BasicStrategyEntity::getRecommendDesc);
        if (strategySearchBO.getCity() != null) {
            wrapper.eq(BasicStrategyEntity::getCity, strategySearchBO.getCity());
        }
        if (strategySearchBO.getProvince() != null) {
            wrapper.eq(BasicStrategyEntity::getProvince, strategySearchBO.getProvince());
        }
        if (strategySearchBO.getIsPlatform() != null) {
            wrapper.eq(BasicStrategyEntity::getIsPlatform, strategySearchBO.getIsPlatform());
        }
        EsPageInfo<BasicStrategyEntity> pageQuery = basicStrategyEntityMapper.pageQuery(wrapper, strategySearchBO.getPageIndex(), strategySearchBO.getPageSize());
        if (pageQuery == null || CollectionUtils.isEmpty(pageQuery.getList())) {
            log.warn("BasicStrategyService#searchStrategy 进行数据搜索完成，数据为空 入参：{}", JSONObject.toJSONString(strategySearchBO));
            return PageResult.of(Collections.emptyList(), strategySearchBO.getPageIndex(), strategySearchBO.getPageSize(), 0);
        }
        PageResult<BasicStrategyBO> result = new PageResult<>();
        List<BasicStrategyBO> resultList = Lists.newArrayList();
        for (BasicStrategyEntity entity : pageQuery.getList()) {
            BasicStrategyBO basicStrategyBO = StrategyConvert.convertToBasicStrategyBO(entity);
            resultList.add(basicStrategyBO);
        }
        result.setResults(resultList);
        result.setTotal(pageQuery.getTotal());
        result.setHasNext(pageQuery.isHasNextPage());
        result.setPageNum(strategySearchBO.getPageIndex());
        result.setPageSize(strategySearchBO.getPageSize());
        Map<Integer, List<BasicStrategyBO>> groupCollect = resultList.stream().collect(Collectors.groupingBy(BasicStrategyBO::getIsPlatform));
        for (Integer key : groupCollect.keySet()) {
            List<BasicStrategyBO> searchIdList = groupCollect.get(key);
            List<Long> collect = searchIdList.stream().map(BasicStrategyBO::getId).collect(Collectors.toList());
            log.info("BasicStrategyService#searchStrategy 进行数据搜索完成，入参：{},分类数据：{},结果IdList：{}", JSONObject.toJSONString(strategySearchBO), key, JSONObject.toJSONString(collect));
        }
        return result;
    }
}
